﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using Microsoft.Win32;
using GuestList.Commands;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

namespace GuestList.ViewModels
{
    public class OptionsVM : NotifyPropertyChanged
    {
        public event EventHandler Close;

        Dictionary<string, SettingsPropertyValueCollection> properties = new Dictionary<string, SettingsPropertyValueCollection>();
        
        public string SaveLocation
        {
            get { return UserSettings.Default.SaveLocation; }
            set
            {
                UserSettings.Default.SaveLocation = value;
                Notify("SaveLocation");
            }
        }

        public ObservableCollection<FontFamily> FontFamilies
        {
            get { return new ObservableCollection<FontFamily>(Fonts.SystemFontFamilies); }
        }

        public FontFamily FontFamily
        {
            get { return UserSettings.Default.PrintFontFamily; }
            set
            {
                UserSettings.Default.PrintFontFamily = value;
                Notify("FontFamily");
            }
        }

        public double FontSize
        {
            get { return UserSettings.Default.PrintFontSize; }
            set
            {
                UserSettings.Default.PrintFontSize = value;
                Notify("FontSize");
            }
        }

        public FontStyle FontStyle
        {
            get { return UserSettings.Default.PrintFontStyle; }
            set
            {
                UserSettings.Default.PrintFontStyle = value;
                Notify("FontStyle");
            }
        }

        public FontCapitals FontCapitals
        {
            get { return UserSettings.Default.PrintFontCapitals; }
            set
            {
                UserSettings.Default.PrintFontCapitals = value;
                Notify("FontCapitals");
            }
        }

        public ObservableCollection<FontCapitals> AllFontCapitals
        {
            get { return new ObservableCollection<FontCapitals>(Enum.GetValues(typeof(FontCapitals)).Cast<FontCapitals>()); }
        }

        public RelayCmd SaveCmd { get { return new RelayCmd(SaveChanges, AreSettingsValid); } }
        public RelayCmd CancelCmd { get { return new RelayCmd(RevertChanges, () => true); } }
        public RelayCmd SelectFileCmd { get { return new RelayCmd(SelectFile, () => true); } }
        //public RelayCmd ChangeFontCmd { get { return new RelayCmd(ChangeFont, () => true); } }

        public OptionsVM()
        {
            foreach (SettingsProperty p in UserSettings.Default.Properties)
            {
                properties.Add(p.Name, p.Provider.GetPropertyValues(UserSettings.Default.Context, UserSettings.Default.Properties));
            }
        }

        //private void ChangeFont()
        //{
        //    var dialog = new System.Windows.Forms.FontDialog
        //    {
        //        Font = UserSettings.Default.PrintFont
        //    };
        //    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //    {
        //        PrintFont = dialog.Font;
        //    }
        //}

        private bool AreSettingsValid()
        {
            return File.Exists(UserSettings.Default.SaveLocation);
        }

        private void SaveChanges()
        {
            UserSettings.Default.Save();
            OnClosing(EventArgs.Empty);
        }

        private void RevertChanges()
        {
            foreach (SettingsProperty p in UserSettings.Default.Properties)
            {
                p.Provider.SetPropertyValues(UserSettings.Default.Context, properties[p.Name]);
            }
            OnClosing(EventArgs.Empty);
        }

        private void SelectFile()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.FileOk += delegate
            {
                SaveLocation = dialog.FileName;
            };
            dialog.ShowDialog();
        }

        protected void OnClosing(EventArgs e)
        {
            if (Close != null)
                Close(this, e);
        }
    }
}
